/**
 * Copyright 2008 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kiff.util.parser;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

/**
 * Tokenizes a character stream for parsing.
 * @author Adam
 * @version $Id$
 * 
 * Created on Dec 25, 2008 at 10:09:09 PM 
 */
public class StringTokenizer implements Tokenizer {
	/**
	 * The input stream for the tokenizer to work on.
	 */
	private Reader reader;

	/**
	 * The character that was read from the last next operation.
	 */
	private int character;

	/**
	 * This flag will be set to false if there is a character is remaining to be processed.
	 */
	private boolean remainder = false;

	/**
	 * Reads the next character in the stream.
	 * @throws IOException If an I/O error occurs.
	 */
	private int next() throws IOException {
		character = reader.read();
		return character;
	}

	/**
	 * Constructs a Tokenizer with a String.
	 * @param stream a string.
	 */
	public StringTokenizer(final String stream) {
		this.reader = new StringReader(stream);
	}

	/**
	 * {@inheritDoc}
	 * @see kiff.util.parser.Tokenizer#nextToken()
	 */
	public Token nextToken() throws IOException {
		if (remainder) {
			remainder = false;
			return new Token(character);
		}
		next();
		if (Character.isLetter(character) || Character.isDigit(character)) {
			StringBuilder buffer = new StringBuilder();
			buffer.append((char) character);
			while (next() != Type.EOF) {
				if (!Character.isLetter(character) && !Character.isDigit(character)) {
					remainder = true; // Process the current character in the next nextToken call.
					return new Word(buffer.toString());
				} else {
					buffer.append((char) character);
				}
			}
			remainder = true; // Process the current character in the next nextToken call.
			return new Word(buffer.toString());
		}
		return new Token(character);
	}

}
